﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BLL;
using ViewModels;

namespace LibraryMIS.UC
{
    public partial class ManageBooks : UserControl
    {
        private BookBLL book;//定义BookBLL业务逻辑访问类book
        private DataSet ds;//DataSet数据集用于填充图书列表（DataGridView）
        private string selectedId;//存储列表中选定行的ID

        private string opration = "I";//定义操作标识。I：新增，U：修改

        public ManageBooks()
        {
            InitializeComponent();
            book = new BookBLL();        
        }
        #region 图书信息列表实现
        private void GetBookList()
        {
            ds = book.GetBookList();
            dgvBooksList.DataSource = ds.Tables[0];//将DataSet数据集绑定到DataGridView数据源中
        }
        #endregion

        private void ManageBooks_Load(object sender, EventArgs e)
        {
            GetBookList();
            GetBookClasses(cbBookClass);
        }
        #region 搜索功能实现
        private void btnSearching_Click(object sender, EventArgs e)
        {
            string inputS = this.txtSearchStr.Text.Trim();//获取搜索输入框的文本信息，同时进行空格清除Trim
            int selectedSearching = this.cbDropList.SelectedIndex;//获取搜索下拉索引值

            //如果没有进行搜索内容输入，提示用户
            if (String.IsNullOrEmpty(inputS))
            {
                MessageBox.Show("请进行搜索内容的输入！", "系统提示");
                this.txtSearchStr.Focus();//光标闪烁
            }
            //如果没有进行下拉选择，提示用户
            if (this.cbDropList.SelectedIndex == -1)
            {
                MessageBox.Show("请进行下拉内容的选择！", "系统提示");
            }

            if (selectedSearching == 0)
            {
                ds = book.GetBookListByID(inputS);
                dgvBooksList.DataSource = ds.Tables[0];
            }
            if (selectedSearching == 1)
            {
                ds = book.GetBookListByName(inputS);
                dgvBooksList.DataSource = ds.Tables[0];
            }
            if (selectedSearching == 2)
            {
                ds = book.GetBookListByAuthor(inputS);
                dgvBooksList.DataSource = ds.Tables[0];
            }
        }
        #endregion

        #region 图书分类下拉菜单实现
        private void GetBookClasses(ComboBox controlName)
        {
            DataSet bookClasses = book.GetBookClasses();//调用业务逻辑类，实现图书类别数据集读取
            controlName.DataSource = bookClasses.Tables[0];//将数据集绑定至传入的ComboBox组件
            controlName.DisplayMember = "BookClass";
            controlName.ValueMember = "ClassID";
        }
        private void cbBookClass_VisibleChanged(object sender, EventArgs e)
        {
            try
            {
                GetBookClasses(cbBookClass);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "系统提示");
            }
        }
        #endregion

        private void btnAdd_Click(object sender, EventArgs e)
        {
            string bookid=this.txtBookID.Text.Trim();
            string bookname = this.txtBookName.Text.Trim();
            string isbn = this.txtISBN.Text.Trim();
            string author = this.txtAuthor.Text.Trim();
            string bookversion = this.txtBookVersion.Text.Trim();
            string publisher = this.txtPublisher.Text.Trim();

            int wordcount = int.Parse(this.txtWordCount.Text.Trim());
            int pagecount = int.Parse(this.txtPageCount.Text.Trim());
            decimal price = decimal.Parse(this.txtPrice.Text.Trim());

            DateTime publishdate = this.dtpPublishDate.Value.Date;//获取出版日期
            // 获取去下拉菜单的值
            string bookclassid = this.cbBookClass.SelectedValue.ToString().Trim();//获取下拉菜单绑定ID值
            BookVM bookvm = new BookVM()
            {
                BookID=bookid,
                BookName= bookname,
                ISBN=isbn,
                Author=author,
                PublishDate=publishdate,
                BookVersion=bookversion,
                WordCount=wordcount,
                PageCount=pagecount,
                Publisher=publisher,
                Price=price,
                ClassID =bookclassid
            };
            bookvm.BookClasseVM = new BookClasseVM();
            bookvm.BookClasseVM.ClassID = bookclassid;
            bookvm.BookClasseVM.BookClass = book.GetBookClassName(bookclassid);

            book = new BookBLL();
            if(this.btnAdd.Text== "新增")
            {
                object obj = book.EditBook(bookvm, opration);//调用修改业务逻辑方法进行插入数据实现
                if (obj.ToString() == "该书已在库有记录，请勿重复插入！")
                    MessageBox.Show(obj.ToString(), "系统提示");
                else if(obj.Equals(true))
                    MessageBox.Show("数据操作成功！", "系统提示");
                else
                    MessageBox.Show("数据操作失败！", "系统提示");

            }
        }



    }
}
